<?php
namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\modules\v1\models\Like;
use api\modules\v1\models\News;
use backend\models\User;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
use Yii;

class OpinionController extends U
{
    public $modelClass = 'api\modules\v1\models\Opinion';
    public $type = '';//点赞类型 1:针对主文章赞 2:针对评论赞


    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }


    /*
     * 评论信息
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionCreate(){
        $model = new $this->modelClass();
        $model->attributes = Yii::$app->request->post();
        if($model->save()){
            return [
                "message"=>"反馈提交成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 获取评论列表
     * @param $id
     * @param $catid
     * @return array|\yii\db\ActiveRecord[]
     */
    public function actionList(){
        $model = new $this->modelClass();
        return $model->getList();
    }

    /*
     * 单个评论获取接口
     * @param $comment_id
     * @return mixed
     */
    public function actionOne($o_id){
        $model = new $this->modelClass();
        return $model->getOne($o_id);
    }
}